package com.bawei.movie.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.common.mvi.BaseViewModel
import com.bawei.movie.MovieUiState
import com.bawei.movie.MovieViewIntent
import com.bawei.movie.repos.MovieRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MovieViewModel @Inject constructor(private val repository: MovieRepository): BaseViewModel<MovieViewIntent>() {
    private val _uiState = MutableStateFlow<MovieUiState>(MovieUiState.LoadEmpty) //可变状态流，可以修改
    val uiState: StateFlow<MovieUiState> = _uiState  //不可变，不能进行修改，相当于一get方法

    override fun handIntent(uiIntent: MovieViewIntent) {
        when(uiIntent){
            is MovieViewIntent.LoadMovieType -> getMovieTabs()
            is MovieViewIntent.LoadMovieTypeById -> getMovieTabById(uiIntent.pid)
            is MovieViewIntent.LoadMovieList -> getMovieList(uiIntent.typeId, uiIntent.page, uiIntent.pageSize)
            is MovieViewIntent.LoadRecommendMovieList -> getRecommendMovieList(uiIntent.page, uiIntent.pageSize)
        }
    }

    private fun getMovieTabs(){
        viewModelScope.launch {
            try {
                val result = repository.getMovieTabs()
                if(result.code == 0){
                    _uiState.value = MovieUiState.LoadMovieTypeSuccess(result.data)
                }else{
                    _uiState.value = MovieUiState.LoadMovieTypeFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = MovieUiState.LoadMovieTypeFail("数据加载失败！")
            }
        }
    }

    private fun getMovieTabById(pid: Int){
        viewModelScope.launch {
            try {
                val result = repository.getMultiTypeByPid(pid)
                if(result.code == 0){
                    _uiState.value = MovieUiState.LoadMovieTypeByIdSuccess(result.data)
                }else{
                    _uiState.value = MovieUiState.LoadMovieTypeByIdFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = MovieUiState.LoadMovieTypeByIdFail("数据加载失败！")
            }
        }

    }

    private fun getMovieList(typeId: Int, page: Int, pageSize: Int){
        viewModelScope.launch {
            try {
                val result = repository.getMultiVideoList(typeId, page, pageSize)
                if(result.code == 0){
                    _uiState.value = MovieUiState.LoadMovieListSuccess(result.data)
                }else{
                    _uiState.value = MovieUiState.LoadMovieListFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = MovieUiState.LoadMovieListFail("数据加载失败！")
            }
        }
    }

    private fun getRecommendMovieList(page: Int, pageSize: Int){
        viewModelScope.launch {
            try {
                val result = repository.getRecommendMultiVideo(page, pageSize)
                if(result.code == 0){
                    _uiState.value = MovieUiState.LoadMovieListSuccess(result.data)
                }else{
                    _uiState.value = MovieUiState.LoadMovieListFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = MovieUiState.LoadMovieListFail("数据加载失败！")
            }
        }
    }

}